package com.woniuxy.vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPooled;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/09/10/星期三 10:47
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/redis")
public class RedisVectorController {
    private Logger logger = LoggerFactory.getLogger(RedisVectorController.class);
    @Autowired
    private RedisVectorStore vectorStore;
    @Autowired
    private ChatClient client;
    @Autowired
    private ChatClient chatClient;

    public RedisVectorController() {
        logger.info("初始化本地向量存储");

        //方案一：通过源码判断需要JedisPooled对象，同时这个对象，不是个bean
//        this.vectorStore = RedisVectorStore.builder(new JedisPooled("192.168.115.5", 6380),embeddingModel).build();
    }
    @GetMapping("/add")
    public String add(@RequestParam String doc) throws IOException {
        //1.文档内容，通过代码处理正则过滤 # ## ###
        String[] split = doc.split("#+");
        List<Document> documents = new ArrayList<>();
        int i =0;
        for (String s : split) {
            //后面的元数据，是用来快速区分文档的。
            if(i++%2  == 0) {
                documents.add(new Document(s,Map.of("name","蜗牛学苑")));
            }else {
                documents.add(new Document(s,Map.of("name","马蜂窝")));
            }
        }
        vectorStore.add(documents);
        return "success";
    }
    @GetMapping("/search")
    public String search(@RequestParam String ask){
        List<Document> documents = vectorStore.similaritySearch(SearchRequest.builder()
                .query(ask)
                .topK(5) //返回2条
                .similarityThreshold(0.5) //相似度阈值
                .build());
        return client.prompt(documents.toString()).user(ask).call().content();
    }
    @DeleteMapping("/delete")
    public String delete(){
        //第一种：忽略前缀，直接通过key的名字来进行删除
        vectorStore.delete(List.of("294969a5-1408-4d37-890c-a5be9957683c"));
        //第二种：删除name为马蜂窝的数据
//        FilterExpressionBuilder filterExpressionBuilder = new FilterExpressionBuilder();
//        Filter.Expression build = filterExpressionBuilder.in("content","AI").build();
//        vectorStore.getJedis().ftSearch("woniuxy","AI");//√
//        vectorStore.getJedis().ftSearch("woniuxy","国外的");  //X
//        vectorStore.getJedis().ftSearch("woniuxy","国外的框架"); //√
//        vectorStore.delete(build);
        return "success";
    }
}
